package com.zjj.bams.server;

import com.zjj.bams.entity.Account;
import com.zjj.bams.exception.*;
import com.zjj.bams.service.Bank;
import com.zjj.bams.exception.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
/**
 * ServerTasker  "银行业务员"
 *   1.读取客户端发送来的指令            BufferedReader
 *   2.解析指令，根据指令来执行Bank业务   Bank
 *   3.写回执行的结果 （成功则返回Account账户信息，失败则返回String异常信息） ObjectOutputStream
 */
public class ServerTasker implements Runnable{

    Bank bank;
    Socket socket;

    public ServerTasker(Bank bank , Socket socket){
        this.bank = bank;
        this.socket = socket;
    }

    @Override
    public void run() {
        try (
                //读指令
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //写结果
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        ){
            String line = null;
            Account acc = null;

            //2.读取客户端发送的指令
            while ((line = br.readLine())!=null){
                System.out.println("指令 line = "+line);
                //解析指令
                String[] splits = line.split("#");
                switch (splits[0]){
                    case "login":
                        try {
                            acc = bank.login(Long.valueOf(splits[1]) , splits[2]);
                            if(acc!=null){
                                //3.写回执行登录成功的结果 - Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        } catch (NumberFormatException e) {
                            //3.写回执行登录失败的结果 - String异常信息给客户端
                            oos.writeObject("登录账号信息不合法");
                            oos.flush();
                        } catch (NotFoundException | LoginException e) {
                            //3.写回执行登录失败的结果 - String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        break;
                    case "register":
                        break;
                    case "transfer":   //转账
                        try {
                            boolean flag = bank.transfer(Long.valueOf(splits[1]) , splits[2] , Long.valueOf(splits[3]) , Double.parseDouble(splits[4]));
                            if(flag){
                                //3.写回执行转账成功的结果 - Account信息给客户端
                                acc = bank.login(Long.valueOf(splits[1]) , splits[2]);
                                if(acc!=null){
                                    oos.writeObject(acc);
                                    oos.flush();
                                }
                            }
                        } catch (NumberFormatException e) {
                            //3.写回执行登录失败的结果 - String异常信息给客户端
                            oos.writeObject("账号信息不合法");
                            oos.flush();
                        } catch (NotFoundException | MoneyException | TransferException | BalanceNotEnoughException | LoginException e) {
                            //3.写回执行登录失败的结果 - String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        break;
                    case "close":
                        //为了避免客户端关闭时的阻塞 ， 关闭时给予空响应
                        oos.writeObject("");
                        oos.flush();
                        break;
                }
            }

        } catch (SocketException e) {

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
